package com.rd.server.framework.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.keygen.KeyGenerators;
import com.mybatisflex.core.query.QueryColumnBehavior;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisFlexConfiguration {

    public MyBatisFlexConfiguration() {

        // 使用内置规则自动忽略 null 和 空字符串
        QueryColumnBehavior.setIgnoreFunction(QueryColumnBehavior.IGNORE_EMPTY);
        // 使用内置规则自动忽略 null 和 空白字符串
        QueryColumnBehavior.setIgnoreFunction(QueryColumnBehavior.IGNORE_BLANK);

        FlexGlobalConfig config = FlexGlobalConfig.getDefaultConfig();

        config.setLogicDeleteColumn("del_flag");

//        GlobalInsertListener mybatisInsertListener = new GlobalInsertListener();
//        GlobalUpdateListener mybatisUpdateListener = new GlobalUpdateListener();
//        //设置BaseEntity类启用
//        Class[] clazz = {ContractLedgerPO.class, ContractSummaryPO.class};
//        config.registerInsertListener(mybatisInsertListener, clazz);
//        config.registerUpdateListener(mybatisUpdateListener, clazz);


        // ---------  这个配置不生效 还是需要写,都是天华的错  ----------
        // @Id(keyType = KeyType.Generator, value = KeyGenerators.snowFlakeId)
        // 全局ID 配置
        FlexGlobalConfig.KeyConfig keyConfig = new FlexGlobalConfig.KeyConfig();
        keyConfig.setKeyType(KeyType.Generator);
        keyConfig.setValue(KeyGenerators.snowFlakeId);
        keyConfig.setBefore(true);
        config.setKeyConfig(keyConfig);
    }

    @Bean
    public ObjectMapper serializingObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS);
        objectMapper.registerModule(new JavaTimeModule());
        return objectMapper;
    }
}